package com.somsoft.handjc.activity.jiancha.xuncha;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

import com.somsoft.handjc.activity.MainActivity;
import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.activity.query.xuncha.ZhongHeQueryActivity;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XunChaDataUtil;
import com.somsoft.handjc.xuncha.po.ZhongHe;

public class ZhongHeJcActivity extends ExpandableListActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		//设这标题
		setTitle("综合监察");
		setContentView(R.layout.xuncha_jiancha_main); 
         
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();//组名 
        Map<String,String> g1 = new HashMap<String,String>(); 
        g1.put("group", "时限监察"); 
        Map<String,String> g2 = new HashMap<String,String>(); 
        g2.put("group", "异常监察"); 
        Map<String,String> g3 = new HashMap<String,String>(); 
        g3.put("group", "巡查监察"); 
        list.add(g1); 
        list.add(g2);
        list.add(g3);

        List<List<Map<String,String>>> childs = new  ArrayList<List<Map<String,String>>>();//将两个child加入的集合中
        try {
        	Intent it = getIntent();
        	List<NameValuePair> params = new ArrayList<NameValuePair>();
        	String xc_jc_deptName = it.getStringExtra("xc_jc_deptName");
        	String xj_jc_startDate = it.getStringExtra("xj_jc_startDate");
        	String xj_jc_endDate = it.getStringExtra("xj_jc_endDate");
        	
        	if(xc_jc_deptName!=null && !"".equals(xc_jc_deptName)){
				params.add(new BasicNameValuePair("xc_jc_deptName",xc_jc_deptName));
			}
        	if(xj_jc_startDate!=null && !"".equals(xj_jc_startDate) && xj_jc_endDate!=null && !"".equals(xj_jc_endDate)){
				params.add(new BasicNameValuePair("xj_jc_startDate",xj_jc_startDate));
				params.add(new BasicNameValuePair("xj_jc_endDate",xj_jc_endDate));
			}
        	
        	List<ZhongHe> zhongHeJc = XunChaDataUtil.getZhongHeJc(params);
        	
        	for(int i=0;i<zhongHeJc.size();i++){
				ZhongHe zh = zhongHeJc.get(i);
				List<Map<String,String>> listChild = new ArrayList<Map<String,String>>();//child 
		        Map<String,String> map3 = new HashMap<String,String>(); 
		        map3.put("country", "正常数量："+zh.getZcCount()+"；\n所占百分比："+zh.getZcPercent()); 
		        listChild.add(map3); 
		        Map<String,String> map4 = new HashMap<String,String>(); 
		        map4.put("country", "黄牌数量："+zh.getYellowCount()+"；\n所占百分比："+zh.getYellowPercent()); 
		        listChild.add(map4); 
		        Map<String,String> map5 = new HashMap<String,String>(); 
		        map5.put("country", "红牌数量："+zh.getRedCount()+"；\n所占百分比："+zh.getRedPercent()); 
		        listChild.add(map5); 
		        childs.add(listChild);
			}
		} catch (Exception e) {
			Toast.makeText(ZhongHeJcActivity.this, "连接服务器超时！", Toast.LENGTH_SHORT).show();
		}
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, list, R.layout.xuncha_jiancha_group, new String[]{"group"}, 
                new int[]{R.id.tv_group}, childs, R.layout.xuncha_jiancha_child, new String[]{"country"}, new int[]{R.id.tv_child}); 
        setListAdapter(adapter);//适配器 
        Utils.closeLoading();
	}
	
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		//Toast.makeText(this, " 组元素索引: " + groupPosition + " 子元素索引: " + childPosition, Toast.LENGTH_SHORT).show();
		Utils.openLoading(this);
		Intent it = new Intent(ZhongHeJcActivity.this,ZhongHeQueryActivity.class);
		if(groupPosition==0 && childPosition==0){
			it.putExtra("timeLimitJcResult", "NORMAL");
		}else if(groupPosition==0 && childPosition==1){
			it.putExtra("timeLimitJcResult", "YELLOW");
		}else if(groupPosition==0 && childPosition==2){
			it.putExtra("timeLimitJcResult", "RED");
		}else if(groupPosition==1 && childPosition==0){
			it.putExtra("abnormalJcResult", "NORMAL");
		}else if(groupPosition==1 && childPosition==1){
			it.putExtra("abnormalJcResult", "YELLOW");
		}else if(groupPosition==1 && childPosition==2){
			it.putExtra("abnormalJcResult", "RED");
		}else if(groupPosition==2 && childPosition==0){
			it.putExtra("patrolJcResult", "NORMAL");
		}else if(groupPosition==2 && childPosition==1){
			it.putExtra("patrolJcResult", "YELLOW");
		}else if(groupPosition==2 && childPosition==2){
			it.putExtra("patrolJcResult", "RED");
		}
		startActivity(it);
		
		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {			
		menu.add(Menu.NONE, 10, 10, "首页").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(Menu.NONE, 20, 20, "查询").setIcon(android.R.drawable.ic_menu_search);
		menu.add(Menu.NONE, 90, 90, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;											
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		// 选中某个菜单项
		Intent it = null;
		switch (item.getItemId()) {								// 判断菜单项ID
		case  10:
			it = new Intent(ZhongHeJcActivity.this,MainActivity.class);
			startActivity(it);
			break;
		case  20:
			it = new Intent(ZhongHeJcActivity.this,ZhongHeJcQueryActivity.class);
			startActivity(it);
			break;
		case  90:
			MyApplication.getInstance().exit();
			break;
		}
		return false;
	}
}
